package org.contextsensitivetodo;

import java.util.ArrayList;

import org.contextsensitivetodo.context.ContextDelivery;
import org.contextsensitivetodo.context.UserContext;
import org.contextsensitivetodo.context.UserContextAdapter;
import org.contextsensitivetodo.context.configuration.UserContextConfiguration;
import org.contextsensitivetodo.context.updater.ContextUpdaterEngineService;

import android.app.ListActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ShowContextActivity extends ListActivity {
	
	
	private ArrayList<UserContext> userContexts = null;
    private UserContextAdapter adapter;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.userContexts = new ArrayList<UserContext>();
		this.adapter = new UserContextAdapter(this, R.layout.row, userContexts);
        setListAdapter(this.adapter);
    
        
        
		//temp:
		ContextDelivery.getInstace().setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
		
		UserContextConfiguration.getInstance().setOutputTextForDebug((TextView) findViewById(R.id.ContextView));
		UserContextConfiguration.getInstance().setShowContextActivity(this);
		final Handler handler = new Handler();
		UserContextConfiguration.getInstance().setUIThreadCallBackHandler(handler);
		
		
		Button refreshButton = (Button) findViewById(R.id.RefreshContextButton);

		refreshButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				TextView contextInfo = (TextView) findViewById(R.id.ContextView);
				contextInfo.setText(ContextDelivery.getInstace()
						.getCurrentContextAsTextForDebug());
				
				updateListContent(ContextDelivery.getInstace().getContextAsList());
			}
		});

		Button startServiceButton = (Button) findViewById(R.id.StartUpdateService);

		startServiceButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				startService(new Intent(ShowContextActivity.this,
						ContextUpdaterEngineService.class));
			}
		});
		
		Button stopServiceButton = (Button) findViewById(R.id.StopServiceButton);
		
			
		stopServiceButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				stopService(new Intent(ShowContextActivity.this,
						ContextUpdaterEngineService.class));
			}
		});
	}
	
	
	
	private void updateListContent(ArrayList<UserContext> userContexes){

		for (UserContext userContext : this.userContexts) {
			this.adapter.remove(userContext);
		}
		
		this.adapter.notifyDataSetChanged();
		
		
		for (UserContext userContext : userContexes) {
			this.adapter.add(userContext);
		}
		this.adapter.notifyDataSetChanged();	
		this.userContexts = userContexes;

	}
	
	public void setUserContextList(ArrayList<UserContext> userContexes){
		
		this.updateListContent(userContexes);
				
		final NotificationManager mgr; 
		mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		Notification notif = new Notification();
		notif.defaults |= Notification.DEFAULT_SOUND;
		
		mgr.notify(111, notif);
	}
	
}